{% extends '::base.html.twig' %}

{% block body %}
<div class="content">
    <div class="form-row row-fluid" id="div_form_bon_retour">
        <form id="form_bon_retour" method="post" {{ form_enctype(form) }}>
            <div class="span12">
                <div class="box gradient">
                    <div class="title" style="text-align: center">
                        <h4>Bon Livraison Retour</h4>
                    </div>
                    <div class="content noPad clearfix">
                        <table style="width: 100%">
                            <tr>
                                <td style="width: 20%">
                                    <label>Du:</label>
                                    <div>
                                    {{ form_widget(form.date)}}{{ form_widget(form._token)}}
                                        </div>
                                    </td>
                                    <td  style="width: 20%">
                                        <label>Client:</label>
                                        <div  style="width: 200px">
                                    {{ form_widget(form.client,{'attr':{'class':'nostyle','style':'width:100%'}})}}
                                            </div> 
                                        </td>
                                        <td  style="width: 20%">
                                            <label>Departement:</label>
                                            <div style="width: 200px">
                                    {{ render(controller('GdsVenteBundle:Devis:getAllDepartement')) }}
                                                </div> 
                                            </td>
                                            <td  style="width: 40%">
                                                <label>Numero BL Retour:</label>
                                {{ form_widget(form.reference,{'attr':{'class':'span8 text','readonly':'readonly','style':'width:200px'}})}}
                                            </td>
                                        </tr>
                                    </table>
                                    <div class="box " >
                                        <div class="leftBox">
                                            <div class="searchBox"><input type="text" id="box1Filter" class="searchField" placeholder="filter ..." /><button id="box1Clear" type="button" class="btn"><span class="icon12 entypo-icon-cancel"></span></button></div>
                                            <select id="box1View" multiple="multiple" class="multiple nostyle" style="height:300px;">
                                            </select>
                                            <br />
                                            <div class="dn"><select id="box1Storage" name="box1Storage" class="nostyle"></select></div>
                                        </div>
                                        <div class="dualBtn">
                                            <button id="to2" type="button" class="btn"><span class="icon12 minia-icon-arrow-right-3"></span></button>
                                            <button id="allTo2" type="button" class="btn"><span class="icon12 iconic-icon-last"></span></button>
                                            <button id="to1" type="button" class="btn marginT5"><span class="icon12 minia-icon-arrow-left-3"></span></button>
                                            <button id="allTo1" type="button" class="btn marginT5"><span class="icon12 iconic-icon-first"></span></button>
                                        </div>
                                        <div class="rightBox">
                                            <div class="searchBox"><input type="text" id="box2Filter" class="searchField" placeholder="filter ..." /><button id="box2Clear" type="button" class="btn"><span class="icon12 entypo-icon-cancel"></span></button></div>
                                            <select id="box2View" multiple="multiple" class="multiple nostyle" style="height:300px;"></select><br />
                                            <div class="dn"><select id="box2Storage" class="nostyle"></select></div>
                                        </div>
                                    </div> 
                                </div> 
                            </div>
                        </div>
                    </form>
                    <div class="span2" style="margin-right: 0">
                        <table>
                            <tr>
                                <td style="text-align: center">
                                    <input type="button" style="width:100px" id="submit_form_bon_retour" value="Suivant" class="nostyle btn btn-success btn-large" />
                                </td>
                        </table>
                    </div>  
                </div>
            </div>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#departement').val({{user.departement.id}})
                    $('#departement').attr('disabled', true);
                    $('#gds_ventebundle_bonlivraisonretourclienttype_date').datepicker();
                    var fullDate = new Date();
                    bon_livraisons = new Array()
                    var twoDigitMonth = fullDate.getMonth()
                    if (twoDigitMonth == 0)
                        twoDigitMonth = 1
                    twoDigitMonth += "";
                    if (twoDigitMonth.length == 1)
                        twoDigitMonth = "0" + twoDigitMonth;
                    var twoDigitDate = fullDate.getDate() + "";
                    if (twoDigitMonth == "01")
                        twoDigitDate = "01"
                    if (twoDigitDate.length == 1)
                        twoDigitDate = "0" + twoDigitDate;
                    var datePrev = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
                    $("#gds_ventebundle_bonlivraisonretourclienttype_date").val(datePrev)
                    $('#form_bon_retour').validate();
                    $('#submit_form_bon_retour').click(function() {
                        if ($('#form_bon_retour').valid()) {
                            BonRetour()
                        }
                    })
                    date = changeFormatDate($('#gds_ventebundle_bonlivraisonretourclienttype_date').val())
                    client = $('#gds_ventebundle_bonlivraisonretourclienttype_client').val()
                    departement = $('#departement').val()
                    $('#gds_ventebundle_bonlivraisonretourclienttype_date').change(function() {
                        date = changeFormatDate($('#gds_ventebundle_bonlivraisonretourclienttype_date').val())
                        getBonLivraisons(date, client, departement)
                    })
                    $('#gds_ventebundle_bonlivraisonretourclienttype_client').change(function() {
                        client = $('#gds_ventebundle_bonlivraisonretourclienttype_client').val()
                        getBonLivraisons(date, client, departement)
                    })
                    $('#departement').change(function() {
                        departement = $('#departement').val()
                        getBonLivraisons(date, client, departement)
                    })
                    getBonLivraisons(date, client, departement)
                });
                function getBonLivraisons(date, client, departement) {
                    $('#box1View option').remove()
                    $('#box2View option').remove()
                    $.ajax({
                        url: '{{path('gds_vente_get_bon_livraisons')}}',
                        data: 'date=' + date + '&client=' + client + '&departement=' + departement,
                        success: function(data) {
                            for (var i = 0; i < data.length; i++) {
                                $('#box1View').append('<option value="' + data[i]['id'] + '">' + data[i]['reference'] + '</option>');
                                bon_livraisons[i] = data[i]['id']
                            }
                        }
                    })
                }
                function BonRetour() {
                    var ids = new Array()
                    $('#box2View option').each(function() {
                        ids[ids.length] = $(this).val()
                    })
                    if (verifierIdsIn(ids)) {
                        $.ajax({
                            url: '{{path('gds_vente_bon_retour')}}',
                            datatype: 'json',
                            data: 'ids=' + JSON.stringify(ids),
                            success: function(data) {
                                $('#div_form_bon_retour').html(data)
                            }
                        })
                    } else {
                        alert('Erreur')
                    }
                }
                function changeFormatDate(date) {
                    if (date == '')
                        return date
                    date = date.split('/')
                    date = date[2] + '-' + date[1] + '-' + date[0]
                    return date
                }
                function verifierIdsIn(ids) {
                    for (var i = 0; i < ids.length; i++) {
                        var found = false
                        var j = 0
                        while (!found && j < bon_livraisons.length) {
                            if (ids[i] == bon_livraisons[j])
                                found = true
                            j++
                        }
                        if (!found)
                            return false
                    }
                    return true
                }
                </script>
{% endblock %}    